package com.industrial.premu.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuCompanyInfo;
import com.industrial.premu.domain.PreMuDownshift;
import com.industrial.premu.domain.sqsj.PreMuImportVeto;
import com.industrial.premu.mapper.PreMuCompanyInfoMapper;
import com.industrial.premu.mapper.sqsj.PreMuImportVetoMapper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.industrial.premu.mapper.PreMuRejectMapper;
import com.industrial.premu.domain.PreMuReject;
import com.industrial.premu.service.IPreMuRejectService;

/**
 * 否决项Service业务层处理
 * 
 * @author ZhouS
 * @date 2024-01-24
 */
@Service
public class PreMuRejectServiceImpl implements IPreMuRejectService 
{
    @Autowired
    private PreMuRejectMapper preMuRejectMapper;

    private static final int BATCH_SIZE = 300;

    @Autowired
    private PreMuCompanyInfoMapper preMuCompanyInfoMapper;

    @Autowired
    private PreMuImportVetoMapper preMuImportVetoMapper;

    /**
     * 查询否决项
     * 
     * @param id 否决项主键
     * @return 否决项
     */
    @Override
    public PreMuReject selectPreMuRejectById(String id)
    {
        return preMuRejectMapper.selectPreMuRejectById(id);
    }

    /**
     * 查询否决项列表
     * 
     * @param preMuReject 否决项
     * @return 否决项
     */
    @Override
    public List<PreMuReject> selectPreMuRejectList(PreMuReject preMuReject)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        preMuReject.setCounty(district);
        return preMuRejectMapper.selectPreMuRejectList(preMuReject);
    }

    /**
     * 新增否决项
     * 
     * @param preMuReject 否决项
     * @return 结果
     */
    @Override
    public int insertPreMuReject(PreMuReject preMuReject)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        PreMuReject preMuRejectSelect=new PreMuReject();
        preMuRejectSelect.setCounty(district);
        preMuRejectSelect.setEnterType(preMuReject.getEnterType());
        List<PreMuReject>  list=preMuRejectMapper.selectPreMuRejectList(preMuRejectSelect);
        if(list!=null&&list.size()!=0){
            Integer downShiftNoMax = list.stream().map(a -> {
                String rejectNo = a.getRejectNo();
                return Integer.valueOf(rejectNo);
            }).max(Integer::compare).get();
            preMuReject.setRejectNo(String.valueOf(downShiftNoMax+1));
        }else{
            preMuReject.setRejectNo("1");
        }
        preMuReject.setCounty(district);
        preMuReject.setId(IdUtils.simpleUUID());
        preMuReject.setCreateTime(new Date());
        return preMuRejectMapper.insertPreMuReject(preMuReject);
    }

    /**
     * 修改否决项
     * 
     * @param preMuReject 否决项
     * @return 结果
     */
    @Override
    public int updatePreMuReject(PreMuReject preMuReject)
    {
        return preMuRejectMapper.updatePreMuReject(preMuReject);
    }

    /**
     * 批量删除否决项
     * 
     * @param ids 需要删除的否决项主键
     * @return 结果
     */
    @Override
    public int deletePreMuRejectByIds(String[] ids)
    {
        return preMuRejectMapper.deletePreMuRejectByIds(ids);
    }

    /**
     * 删除否决项信息
     * 
     * @param id 否决项主键
     * @return 结果
     */
    @Override
    public int deletePreMuRejectById(String id)
    {
        return preMuRejectMapper.deletePreMuRejectById(id);
    }




    /**
     * 导出否决项的模版
     *
     * @param paramMap
     * @return
     */
    @Override
    public Workbook createTemplateWb(PreMuReject paramMap) {

        Workbook wb = new SXSSFWorkbook(500);
        XSSFCellStyle headerStyle = (XSSFCellStyle) wb.createCellStyle();
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderTop(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);//設置水平垂直
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);//設置垂直居中
        headerStyle.setWrapText(true); //换行
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);    //设置填充方案
        headerStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(180,198,231)));  //设置填充颜色
        SXSSFSheet sheet = ((SXSSFWorkbook) wb).createSheet("否决项项");
        sheet.createFreezePane( 0, 2, 0, 2 );//要冻结的列数  要冻结的行数  右边区域[可见]的首列序号 下边区域[可见]的首行序号
        CellRangeAddress cra =new CellRangeAddress(0, 1, 0, 0); // 起始行, 终止行, 起始列, 终止列
        sheet.addMergedRegion(cra);
        CellRangeAddress cra2 =new CellRangeAddress(0, 1, 1, 1); // 起始行, 终止行, 起始列, 终止列
        sheet.addMergedRegion(cra2);
        sheet.setColumnWidth(0,500*11); //設置第一列寬度
        sheet.setColumnWidth(1,500*19);//設置第二列寬度

        SXSSFRow row = sheet.createRow(0);
        row.setZeroHeight(true); //隱藏本行
        SXSSFCell usccNameCell = row.createCell(0, CellType.STRING);

        usccNameCell.setCellStyle(headerStyle);
        usccNameCell.setCellValue("统一社会信用代码");
        SXSSFCell companyNameCell = row.createCell(1, CellType.STRING);
        companyNameCell.setCellStyle(headerStyle);
        companyNameCell.setCellValue("企业全称");
        SXSSFRow nameRow = sheet.createRow(1);
        usccNameCell = nameRow.createCell(0, CellType.STRING);
        usccNameCell.setCellStyle(headerStyle);

        companyNameCell = nameRow.createCell(1, CellType.STRING);
        companyNameCell.setCellStyle(headerStyle);
        List<PreMuReject>  vetoLists = preMuRejectMapper.list(paramMap);

        for( int i =0; i<vetoLists.size(); i++){
            sheet.setColumnWidth(i+2,500*24);
            PreMuReject veto =vetoLists.get(i);
            SXSSFCell indicatorNameCell = nameRow.createCell(i+2, CellType.STRING);
            SXSSFCell indicatorIdCell = row.createCell(i+2, CellType.STRING);
            indicatorNameCell.setCellValue(veto.getRejectName());
            indicatorIdCell.setCellValue(veto.getId());
            indicatorIdCell.setCellStyle(headerStyle);
            indicatorNameCell.setCellStyle(headerStyle);
        }

        PreMuCompanyInfo queryParams = new PreMuCompanyInfo();
        queryParams.setYear(paramMap.getYears());
        queryParams.setCompanyType(paramMap.getEnterType());
        queryParams.setCounty(paramMap.getCounty());
        List<PreMuCompanyInfo> comList = preMuCompanyInfoMapper.selectPreMuCompanyInfoList(queryParams);
        for(int i=0;i<comList.size();i++) {
            PreMuCompanyInfo companyInfoDO = comList.get(i);
            SXSSFRow comRow = sheet.createRow(i + 2);
            SXSSFCell usccCell = comRow.createCell(0, CellType.STRING);
            SXSSFCell comNameCell = comRow.createCell(1, CellType.STRING);
            usccCell.setCellValue(companyInfoDO.getUscc());
            comNameCell.setCellValue(companyInfoDO.getCompanyName());
        }

        //设置文本样式
        if(comList!=null &&vetoLists!=null && vetoLists.size()>0 && comList.size()>0 ) {
            String[] strs = new String[]{"是", "否"};
            CellRangeAddressList regions = new CellRangeAddressList(2, comList.size() + 1, 2, vetoLists.size() + 1);
            DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
            DataValidationConstraint createExplicitListConstraint = dataValidationHelper.createExplicitListConstraint(strs);
            DataValidation createValidation = dataValidationHelper.createValidation(createExplicitListConstraint, regions);

            if (createValidation instanceof XSSFDataValidation) {
                createValidation.setSuppressDropDownArrow(true);
                createValidation.setShowErrorBox(true);
            } else {
                createValidation.setSuppressDropDownArrow(false);
            }
            sheet.addValidationData(createValidation);
        }
        return wb;
    }


}
